﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Utils.DatabaseUtils;
using Utils;

namespace Entity
{
    public class CustomerUI
    {

        public Customer Login(string username, string password)
        {
            try
            {
                Customer result = CusInfo(SqlHelper.ExecuteDataset(Common.Conn, "Customer_login", username, password));
                return result;
            }
            catch (Exception e) { return null; }
        }

        private Customer CusInfo(DataSet ds)
        {
            try
            {
                if (TableLib.HasData(ds))
                {
                    DataRow row = ds.Tables[0].Rows[0];
                    Customer cus = new Customer();
                    cus.Cusid = Convert.ToInt32(row["cusid"].ToString().Trim());
                    cus.Username = row["Username"].ToString().Trim();
                    cus.F_Name = row["F_Name"].ToString();
                    cus.L_Name = row["L_Name"].ToString();
                    cus.Dob = Convert.ToDateTime(row["Dob"].ToString());
                    cus.Gender = Convert.ToBoolean(row["Gender"].ToString());
                    cus.P_No = row["P_No"].ToString();
                    cus.Address = row["Address"].ToString();
                    cus.Email = row["Email"].ToString();
                    cus.C_Status = Convert.ToBoolean(row["C_Status"].ToString());

                    return cus;
                }
                return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public int ChangePass(int cusid, string oldPass, string newPass)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Customer_changepass", cusid, oldPass, newPass);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int Edit(int cusid, string Password)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Customer_edit", cusid, Password);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int Active(int cusid,bool C_Status)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Customer_changeactive", cusid, C_Status);
                return result;
            }
            catch (Exception e) { return -1; }
        }

        public int Add(string Username, string Password, string F_Name, string L_Name, DateTime? Dob, bool Gender, string P_No, string Address, string Email, bool C_Status)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Customer_add", Username, Password, F_Name, L_Name, Dob, Gender, P_No, Address, Email, C_Status);
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Update(int cusid, string F_Name, string L_Name, DateTime? Dob, bool Gender, string P_No, string Address, string Email)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Customer_update", cusid, F_Name, L_Name, Dob, Gender, P_No, Address, Email);
                return result;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }

        public int Delete(int cusid)
        {
            try
            {
                int result = SqlHelper.ExecuteNonQuery(Common.Conn, "Customer_delete", cusid);
                    return result;
            }
            catch(Exception ex)
            {
                return -1;
            }
        }

        public DataSet Get(int cusid)
        {
            try
            {
                DataSet result = SqlHelper.ExecuteDataset(Common.Conn, "Customer_get", cusid);
                return result;
            }
            catch (Exception e) { return null; }
        }

        public DataSet GetNewCus(int cusid)
        {
            try
            {
                DataSet result = SqlHelper.ExecuteDataset(Common.Conn, "Customer_getnewcus", cusid);
                return result;
            }
            catch (Exception e) { return null; }
        }

        public DataSet Statistic()
        {
            try
            {
                DataSet result = SqlHelper.ExecuteDataset(Common.Conn, "Customer_statistic");
                return result;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public DataSet GetAll()
        {
            try
            {
                DataSet result = SqlHelper.ExecuteDataset(Common.Conn, "Customer_getall");
                return result;
            }
            catch (Exception e) { return null; }
        }


        //-------------------------------------

    }

    public class Customer
    {
        int cusid;

        public int Cusid
        {
            get { return cusid; }
            set { cusid = value; }
        }

        string _Username;

        public string Username
        {
            get { return _Username; }
            set { _Username = value; }
        }

        string _Password;

        public string Password
        {
            get { return _Password; }
            set { _Password = value; }
        }

        string _F_Name;

        public string F_Name
        {
            get { return _F_Name; }
            set { _F_Name = value; }
        }

        string _L_Name;

        public string L_Name
        {
            get { return _L_Name; }
            set { _L_Name = value; }
        }

        DateTime _Dob;

        public DateTime Dob
        {
            get { return _Dob; }
            set { _Dob = value; }
        }

        bool _Gender;

        public bool Gender
        {
            get { return _Gender; }
            set { _Gender = value; }
        }

        string _P_No;

        public string P_No
        {
            get { return _P_No; }
            set { _P_No = value; }
        }

        string _Address;

        public string Address
        {
            get { return _Address; }
            set { _Address = value; }
        }

        string _Email;

        public string Email
        {
            get { return _Email; }
            set { _Email = value; }
        }

        bool _C_Status;

        public bool C_Status
        {
            get { return _C_Status; }
            set { _C_Status = value; }
        }
    }
}
